home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / cenvid / date.cmm < prev    next >
Encoding:
Text File  |  1995-09-28  |  4.7 KB  |  160 lines

  1. /*
  2. **    date.cmm
  3. **
  4. **    This CMM example file is an implementation of the DOS date
  5. **    command. It is meant to be a part of the CMM windows command
  6. **    interpretor, while at the same time showing the power of CMM.
  7. **    It demonstrates the use of calls into DOS at the interupt level.
  8. **
  9. */
  10. main(argc,argv)
  11. {
  12.    // At least Print the date if it's not DOS or Windows
  13.    if(!defined(_DOS_)&&!defined(_DOS32_)&&!defined(_WINDOWS_))
  14.    {
  15.       printf("%s",asctime(localtime(time())));
  16.       exit(0);
  17.    }
  18.  
  19.    /* get the current system date */
  20.    /* use DOS call 0x2a to get the date */
  21.    regs.ah = 0x2a;
  22.  
  23.    interrupt(0x21,regs, outregs);
  24.  
  25.    //   this is the set of return values from DOS call 2a
  26.    //   Day    = outregs.dl;
  27.    //   Month  = outregs.dh;
  28.    //   Year   = outregs.cx;
  29.    //   dayOfWeek = outregs.al;
  30.  
  31.    /* if the new date wasn't entered, prompt for it */
  32.    if(argc<2)
  33.    {
  34.       printf("Current date is %s %02d-%02d-%d\n",GetDay(outregs.al),
  35.       outregs.dh, outregs.dl, outregs.cx );
  36.  
  37.       printf("Enter new date (mm-dd-yy): ");
  38.       gets(dateString);
  39.    }
  40.    else if (argc == 2)
  41.    {
  42.       /* try the command line argument for a new date */
  43.       dateString = argv[1];
  44.    }
  45.    else 
  46.       GiveInstructionsAndExit();
  47.  
  48.    while(dateString[0] != '\0')
  49.    {
  50.       dateValid = IsDateValid(dateString);
  51.       
  52.       /* if the date isn't valid, let the user know */
  53.       if(dateValid == FALSE)
  54.       {
  55.          dateString[0] = '\0';
  56.          printf("\nInvalid date\n");
  57.          printf("Enter new date (mm-dd-yy): ");
  58.          gets(dateString);
  59.       }
  60.       else
  61.       {
  62.          /* get the pieces of the date */
  63.          sscanf(dateString,"%d-%d-%d",newMonth,newDay,newYear);
  64.  
  65.          /* print the new date */
  66.          printf("New date is %02d-%02d-%d\n",newMonth,newDay,newYear);
  67.  
  68.          /* use DOS call 0x2b to set the new date */
  69.          regs.dl = newDay;
  70.          regs.dh = newMonth;
  71.          regs.cx = newYear;
  72.          regs.ah = 0x2b;
  73.  
  74.          interrupt(0x21,regs);
  75.  
  76.          /* setting the string to null allows us to exit */
  77.          dateString[0] = '\0';
  78.       }
  79.    }
  80. }
  81. GiveInstructionsAndExit()  // Show some info about using this program
  82. {                          // and then exit this program. Do not return.
  83.    printf("date.cmm - CEnvi date utility.\n");
  84.    printf("           This utility requires no more than 1 argument. In general,\n");
  85.    printf("           either the new date mm-dd-yy or no argument. If no new date is \n");
  86.    printf("           the user will be prompted for one. \n");
  87.    printf("           For Example:\n");
  88.    printf("                        date\n");
  89.    printf("                        date 08-05-1966\n");
  90.    exit(EXIT_FAILURE);
  91.  
  92. }
  93.  
  94. /* This function is used to determine if a given date fits the format
  95.    mm-dd-yyyy. It is used for verifying a new system date. 
  96.    On PC's a valid year is between 1980 and 2099 */
  97. IsDateValid(dateString)
  98. {
  99.    dateValid = FALSE;
  100.  
  101.    /* use sscanf to get the parts of the date */
  102.    if(sscanf(dateString,"%d-%d-%d",newMonth,newDay,newYear) == 3)
  103.    {
  104.       /* check the month and the year */
  105.       if((newMonth>0)&&(newMonth<=12)&&(newYear>=1980)&&(newYear<2100))
  106.       {
  107.          /* now check the number of days in the month */
  108.          switch(newMonth)
  109.          {
  110.             /* first the 31 day months */
  111.             case 1:       // January
  112.             case 3:       // March
  113.             case 5:       // May
  114.             case 7:       // July
  115.             case 8:       // August
  116.             case 10:      // October
  117.             case 12:      // December
  118.                if((newDay > 0)&&(newDay <= 31))
  119.                dateValid = TRUE;
  120.                break;
  121.  
  122.             /* Now the 30 day months */
  123.             case 4:       // April
  124.             case 6:       // June
  125.             case 9:       // September
  126.             case 11:      // November
  127.                if((newDay > 0)&&(newDay <= 30))
  128.                dateValid = TRUE;
  129.                break;
  130.  
  131.             case 2:       // February
  132.                // account for leap year 
  133.                if(((newDay > 0)&&(newDay <= 28))||
  134.                   ((newDay > 0)&&(newDay<=29)&&((newYear % 4)==0)))
  135.                      dateValid = TRUE;
  136.                break;
  137.          }
  138.       }
  139.    }
  140.    return(dateValid);
  141. }
  142. /* convert a day represented from 0-6 to a string for display */
  143. GetDay(integerDay)
  144. {
  145.    if(integerDay == 0)
  146.       return("Sun");
  147.    if(integerDay == 1)
  148.       return("Mon");
  149.    if(integerDay == 2)
  150.       return("Tue");
  151.    if(integerDay == 3)
  152.       return("Wed");
  153.    if(integerDay == 4)
  154.       return("Thu");
  155.    if(integerDay == 5)
  156.       return("Fri");
  157.    if(integerDay == 6)
  158.       return("Sat");
  159. }
  160.